/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.util;

import java.io.File;
import java.io.FileOutputStream;

import org.isqlviewer.bookmarks.BookmarkFolder;
import org.isqlviewer.xml.BookmarkDigester;

/**
 * Runnable code for saving bookmark information in a seperate thread.
 * <p>
 * Allows the saving of bookmarks to disk an asynchronous operation.
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class AsynchronousBookmarkSaver extends LoggableObject implements Runnable {

    // TODO add localized messages to this class//
    private BookmarkFolder rootFolder = null;
    private File bookmarksFile = null;

    /**
     * Creates a bookmark saver using the given folder to the default location.
     * <p>
     * 
     * @param rootFolder bookmark folder to save.
     * @see IsqlToolkit#getDefaultBookmarksFile()
     */
    public AsynchronousBookmarkSaver(BookmarkFolder rootFolder) {

        this(rootFolder, IsqlToolkit.getDefaultBookmarksFile());
    }

    /**
     * Creates a bookmark saver to the specified file location, using the given folder.
     * <p>
     * 
     * @param rootFolder bookmark folder to save.
     * @param bookmarksFile the location to save the bookmark folder too.
     */
    public AsynchronousBookmarkSaver(BookmarkFolder rootFolder, File bookmarksFile) {

        if (bookmarksFile == null) {
            throw new NullPointerException(File.class.getName());
        }
        this.rootFolder = rootFolder;
        this.bookmarksFile = bookmarksFile;
    }

    public void run() {

        FileOutputStream fis = null;
        try {
            fis = new FileOutputStream(bookmarksFile);
            BookmarkDigester.writeBookmarkFolder(fis, rootFolder);
            info("Saved bookmarks to :" + bookmarksFile);
        } catch (Exception e) {
            error("error saving bookmarks to file", e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Throwable t) {

                }
            }
            System.gc();
        }
    }

}
